package com.atguigu.web;

import com.atguigu.pojo.Server;
import com.atguigu.pojo.Service;
import com.atguigu.service.ServerService;
import com.atguigu.service.ServiceService;
import com.atguigu.service.impl.ServerServiceImpl;
import com.atguigu.service.impl.ServiceServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

//前端访问这个对象,使用action,抽取数据,打包进server对象,把server对象存入serverService类,存入数据库,返回"服务器首页"
public class ServerServlet extends BaseServlet {
    ServerService serverService=new ServerServiceImpl();
    ServiceService serviceService=new ServiceServiceImpl();
    protected void ajaxCreateServer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String ip=request.getParameter("ip");
        Integer cpu=WebUtils.parseInt(request.getParameter("cpu"),0);
        Integer memory=WebUtils.parseInt(request.getParameter("memory"),0);
        Integer disk=WebUtils.parseInt(request.getParameter("disk"),0);
        Date endTime=WebUtils.parseStringToDate(request.getParameter("endTime"));

        System.out.println(id);
        if(id==null){//新增
            Server server=new Server(WebUtils.id(),name,ip,"",WebUtils.getFreePorts(""),cpu,memory,disk, endTime);
            serverService.addServer(server);
        }else {//更新
            System.out.println("进入serverupdate,id:"+id);
            Server server=serverService.queryServerById(id);
            server.setName(name);
            server.setIp(ip);
            server.setCpu(cpu);
            server.setMemory(memory);
            server.setDisk(disk);
            server.setEndTime(endTime);
            serverService.updateServer(server);
        }
        //Server server=new Server(null,"济宁8","1.1.1.1","4c / 4G",500, WebUtils.parseStringToDate("20210327"));
        //serverService.addServer(server);
        response.getWriter().write("存入成功");
    }
    protected void ajaxDeleteServer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        Server server=serverService.queryServerById(id);
        if(server==null){
            response.getWriter().write("server id: "+id+" not found");
        }else {
            WebUtils.deleteServer(server);//欲删server,必先删service;欲删service,必先删serviceCconfig
        }
        //Server server=new Server(null,"济宁8","1.1.1.1","4c / 4G",500, WebUtils.parseStringToDate("20210327"));
        //serverService.addServer(server);
        response.getWriter().write("删除成功");
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        List<Server> servers= serverService.queryServers();
        request.setAttribute("servers",servers);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
                +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"
                +"已经进入doget方法,"+request.getParameter("action")+" 是action名");
        doPost(request,response);//把dopost方法优化到baseServlet抽象类中去了,使得可以按照request action执行特定的方法
        //由于action判断行为在dopost中,所以在doget中执行一下dopost就可以执行判断了,从而执行特定的方法了.
    }
}
